package cn.com.digitalnet.ad.web;

import java.util.Map;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.multipart.MultipartFile;
import cn.com.digitalnet.ad.base.CrudController;
import cn.com.digitalnet.ad.base.CrudService;
import cn.com.digitalnet.ad.entity.${className};
import cn.com.digitalnet.ad.service.${className}Service;


@Controller
@RequestMapping(value="${path}")
public class ${className}Controller extends CrudController<${className}> {
	
	private static final String PAGE_SIZE = "10";
	
	{
		sortTypes.put("id", "自动");
	}
	
	@Autowired private  ${className}Service service;
	
	@Override
	protected CrudService<${className}> service() {
		return service;
	}

	@Override
	protected String english() {
		return "${english}";
	}

	@Override
	protected String chinese() {
		return "";
	}

	@Override
	protected String parentMenu() {
		return  "";
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String list(@RequestParam(value="page", defaultValue="1")int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "id") String sortType,
			@RequestParam(value = "sortDire", defaultValue = "asc") String sortDire,
			Model model,
			ServletRequest request){
		return standardList(pageNumber, pageSize, sortType, sortDire, model, request);
	}
	
	@RequestMapping(value="/publish/{id}")
	public String publish(@PathVariable("id")Long id,
			RedirectAttributes map){
		return standardSubmit(id, map);
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable("id")Long id,RedirectAttributes redirectAttributes){
		return standardDelete(id, redirectAttributes);
	}
	
	@RequestMapping("/update/{id}")
	public String updateForm(@PathVariable("id")Long id,Model model){
		return standardUpdateForm(id, model);
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(@Valid @ModelAttribute("_entity_") ${className} e,RedirectAttributes redirectAttributes){
		return standardUpdate(e, redirectAttributes);
	}
	
	@RequestMapping("/create")
	public String createForm(Model model){
		return standardCreateForm(model);
	}
	
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String create(@Valid ${className} e,RedirectAttributes redirectAttributes){
		e.setIsenabled(0);
		return standardCreate(e, redirectAttributes);
	}
	
	@RequestMapping("/view/{id}")
	public String view(@PathVariable("id")Long id, Model model){
		return standardView(id, model);
	}
	
	@RequestMapping(value = "import", method = RequestMethod.GET)
	public String importData(Model model){
		return standardImportData(model);
	}

	@RequestMapping(value = "doImport", method = RequestMethod.POST)
	public String doImport(@RequestParam("file") MultipartFile multipartFile,  RedirectAttributes redirectAttributes){
		return standardDoImport(multipartFile, redirectAttributes);
	}
	
	
	@ModelAttribute
	public void getEntity(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		standardGetEntity(id, model);
	}

}
